package com.epro.dx.activity.coupon.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.listenner.OnCouponExpiredListenner;
import com.epro.dx.widget.TimerView;
import com.epro.dx.widget.TimerView.OnTimeOutListener;
import com.weipu.common.facade.model.CouponModel;

/**
 * @author Tony
 * couponMarket列表的数据适配器
 */
public class MyCouponListAdapter extends BaseAdapter{

	private ArrayList<CouponModel> coupons;
	private Context mContext;
	
	private OnCouponExpiredListenner expiredListenner;
	public MyCouponListAdapter(Context mContext,ArrayList<CouponModel> coupons){
		this.mContext = mContext;
		this.coupons = coupons;
		
	}
	@Override
	public int getCount() {
		if(coupons!=null){
			return coupons.size();
		}
		return 0;
	}

	@Override
	public CouponModel getItem(int position) {
		return coupons.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void setOnCouponExpiredListenner(OnCouponExpiredListenner listenner){
		this.expiredListenner=listenner;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
//			convertView = View.inflate(mContext, R.layout.item_mycoupon_list, null);
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_mycoupon_list, null);
			holder.couponInfo = (LinearLayout) convertView.findViewById(R.id.item_mycoupon);
			holder.showLeftDays = (LinearLayout) convertView.findViewById(R.id.item_mycoupon_showleftdays);
			holder.showLeftTimes = (LinearLayout) convertView.findViewById(R.id.item_mycoupon_showlefttimes);
			holder.value = (TextView) convertView.findViewById(R.id.item_mycoupon_value);
			holder.need = (TextView) convertView.findViewById(R.id.item_mycoupon_need);
			holder.timeStart = (TextView) convertView.findViewById(R.id.item_mycoupon_timestart);
			holder.timeEnd = (TextView) convertView.findViewById(R.id.item_mycoupon_timeend);
			holder.desc = (TextView) convertView.findViewById(R.id.item_mycoupon_desc);
			holder.leftDays = (TextView) convertView.findViewById(R.id.item_mycoupon_leftdays);
			holder.timer = (TimerView) convertView.findViewById(R.id.item_mycoupon_timer);
			holder.timer.setTag(position);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		//设置显示内容
		final CouponModel couponModel = coupons.get(position);
		//计算Coupon剩余有效时间(修改，直接从服务器获取剩余时间)
//		long leftDays = calLeftMillis(couponModel.getEndDate());
		int leftDays = couponModel.getEndTotalSeconds();
		holder.timer.setTag(position);
		if(leftDays>0){
			if((leftDays/(60*60*24))<1){
				//剩余不足一天，显示时间倒计时
				
				holder.showLeftDays.setVisibility(View.GONE);
				holder.showLeftTimes.setVisibility(View.VISIBLE);
				String time = turnSeconds2String(leftDays);
				String[] times = time.split(":");
				holder.timer.setTime(Integer.parseInt(times[0]), Integer.parseInt(times[1]), Integer.parseInt(times[2]));
				
				if((Integer)(holder.timer.getTag()) == position){
					holder.timer.startTimer();
				}else{
					holder.timer.reBootTimer();
				}
				//倒计时监听
				holder.timer.setOnTimeOutListener(new OnTimeOutListener() {
					
					@Override
					public void onTimeout() {
						// 当倒计时时间到了后执行以下操作
						if(null!=expiredListenner){
							expiredListenner.onExpired((Integer) holder.timer.getTag());
						}
					}
				});
			}else{
				//剩余时间大于一天显示整天数部分
				holder.showLeftDays.setVisibility(View.VISIBLE);
				holder.showLeftTimes.setVisibility(View.GONE);
				leftDays = (leftDays)/(60*60*24);
				holder.leftDays.setText(leftDays+"");
			}
		}else{
			//Coupon有效期已过，删除
			holder.showLeftDays.setVisibility(View.INVISIBLE);
			holder.showLeftTimes.setVisibility(View.INVISIBLE);
		}
		holder.value.setText(couponModel.getActionDes());
		holder.need.setText(Html.fromHtml(couponModel.getConditionDes()));
		holder.timeStart.setText(couponModel.getBeginDate());
		holder.timeEnd.setText(couponModel.getEndDate());
		holder.desc.setText(couponModel.getDescription());
		/*//设置点击事件
		holder.couponInfo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 点击Coupon内容跳转到相应的活动页面
				System.out.println("跳转到活动页面");
			}
		});*/
		return convertView;
	}
	
	/**
	 * 将秒转换成字符串 时:分:秒 格式
	 * @return
	 */
	public String turnSeconds2String(int seconds){
		long hour = seconds/(60*60);
		long minute = (seconds - hour*60*60)/60;
		long second = seconds - hour*60*60 - minute*60;
		return hour+":"+minute+":"+second;
	}
	
//	/**
//	 * 计算距离指定日期剩余的时间
//	 * @param dates
//	 * @return
//	 */
//	private long calLeftMillis(String dates){
//		long leftMillis = -1;
//		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.ROOT);
//		try {
//			Date date = format.parse(dates);//传递的时间
//			Date nowDate = new Date(System.currentTimeMillis());//现在的时间
//			GregorianCalendar calDate = new GregorianCalendar();  
//			GregorianCalendar calNowDate = new GregorianCalendar();  
//			calDate.setTime(date);  
//			calNowDate.setTime(nowDate);
//			leftMillis = calDate.getTimeInMillis()-calNowDate.getTimeInMillis();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//		return leftMillis;
//	}
	
	class ViewHolder{
		LinearLayout couponInfo;//coupon信息区域，用于点击进入相关的活动页面
		TextView value;//面值
		TextView need;//使用条件
		TextView timeStart;//有效期开始时间
		TextView timeEnd;//有效期截止时间
		TextView desc;//描述信息
		TimerView timer;//倒计时器
		//用于控制显示剩余天数还是剩余时间数
		LinearLayout showLeftDays;
		LinearLayout showLeftTimes;
		TextView leftDays;//剩余天数
	}
}